package com.zdlyh.controller;

import com.zdlyh.domain.ResponseResult;
import com.zdlyh.domain.dto.MenuDto;

import com.zdlyh.domain.entity.Menu;
import com.zdlyh.service.MenuService;
import com.zdlyh.utils.BeanCopyUtils;
import com.zdlyh.utils.SystemConverter;
import com.zdlyh.domain.vo.MenuTreeVo;
import com.zdlyh.domain.vo.MenuVo1;
import com.zdlyh.domain.vo.RoleMenuTreeSelectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @GetMapping("/list")
    public ResponseResult getMenuList(MenuDto menuDto){
        List<MenuVo1> menuList = menuService.getMenuList(menuDto);
        return ResponseResult.okResult(menuList);
    }

    @PostMapping
    public ResponseResult addMenu(@RequestBody Menu menu){
        return menuService.addMenu(menu);
    }

    @GetMapping("/{id}")
    public ResponseResult getMenuById(@PathVariable("id") Long id){
        return menuService.getMenuById(id);
    }

    @PutMapping
    public ResponseResult updateMenu(@RequestBody Menu menu){
        return menuService.updateMenu(menu);
    }

    @DeleteMapping("/{id}")
    public ResponseResult deleteMenuLById(@PathVariable("id") Long id){
        return menuService.deleteMenuById(id);
    }

//      获取菜单下拉树列表
    @GetMapping("/treeselect")
    public ResponseResult getTreeselect(){
        List<MenuVo1> menuList = menuService.getMenuList(new MenuDto());
        List<Menu> menus = BeanCopyUtils.copyBeanList(menuList, Menu.class);
        List<MenuTreeVo> menuTreeVos = SystemConverter.buildMenuSelectTree(menus);
        return ResponseResult.okResult(menuTreeVos);
    }
//    加载对应角色菜单列表树
    @GetMapping("/roleMenuTreeselect/{id}")
    public ResponseResult getTreeselect(@PathVariable("id") Long id){
        List<MenuVo1> menuList = menuService.getMenuList(new MenuDto());
        List<Menu> menus = BeanCopyUtils.copyBeanList(menuList, Menu.class);
        List<Long> checkedKeys = menuService.selectMenuListByRoleId(id);
        List<MenuTreeVo> menuTreeVos = SystemConverter.buildMenuSelectTree(menus);
        RoleMenuTreeSelectVo vo = new RoleMenuTreeSelectVo(checkedKeys,menuTreeVos);
        return ResponseResult.okResult(vo);
    }
}
